
import time
import numba
import numpy as np

# 创建0~99的数组, 转 10 x 10 矩阵
x = np.arange(100).reshape(10,10)

@numba.jit(nopython=True)
def go_fast(a:int) -> float: # Function is compiled and runs in machine code
    trace:float = 0.0
    i: int
    for i in range(a.shape[0]):
        trace += np.tanh(a[i, i])
    return trace

start = time.perf_counter()
ret = go_fast(x)
end = time.perf_counter()
elapsed_time = end - start
print(f"第一次 耗时elapsed time: {elapsed_time:.10f}, ret={ret}")


start = time.perf_counter()
ret = go_fast(x)
end = time.perf_counter()
elapsed_time = end - start
print(f"第二次 耗时elapsed time: {elapsed_time:.10f}, ret={ret}")

"""
第一次 耗时elapsed time: 0.4177319030, ret=8.999999999442107
第二次 耗时elapsed time: 0.0000028000, ret=8.999999999442107
"""